package com.lovedogs.encoding;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Encoder {

	protected final static Log log = LogFactory.getLog(Encoder.class);

	private static MessageDigest digest;

	private Encoder() {
	}

	public static synchronized String getSHA256(String input) {

		StringBuffer sb = new StringBuffer();

		try {
			digest = MessageDigest.getInstance("SHA-256");
			digest.update(input.getBytes());

			byte byteData[] = digest.digest();

			// convert the byte to hex format

			for (int i = 0; i < byteData.length; i++) {
				sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return sb.toString();
	}

}